﻿Imports System.Speech
Imports System.Speech.Recognition
Imports System.Threading



Public Class SpeechRecognition
    ''declare engine


    Dim recog As SpeechRecognitionEngine
    ''declare events
    Public Event AudioLevelUpdated As EventHandler(Of AudioLevelUpdatedEventArgs)
    Public Event SpeechRecognized As EventHandler(Of SpeechRecognizedEventArgs)
    Public Event SpeechRecognitionRejected As EventHandler(Of SpeechRecognitionRejectedEventArgs)


    Public Sub recevent(ByVal sender As System.Object, ByVal e As RecognitionEventArgs)
        Dim dyarr(10) As String
        Dim i As Int64 = 0
        For Each word As RecognizedWordUnit In e.Result.Words
            dyarr(i) = word.Text
            i += 1
        Next

        If (i = 2) Then
            If (e.Result.Confidence > 0.5 And e.Result.Text = "open notepad") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(1))
                    System.Diagnostics.Process.Start(dyarr(1) & ".exe")

                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try


            ElseIf (e.Result.Confidence > 0.8 And e.Result.Text = "close notepad") Then
                Try
                    RichTextBox1.Text = ("closing " & dyarr(1))

                    Shell("taskkill /im notepad.exe")
                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try



            ElseIf (e.Result.Confidence > 0.8 And e.Result.Text = "open wordpad") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(1))
                    System.Diagnostics.Process.Start("wordpad.exe")

                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try
            ElseIf (e.Result.Confidence > 0.8 And e.Result.Text = "close wordpad") Then
                RichTextBox1.Text = ("Close " & dyarr(1))

                Shell("taskkill /im wordpad.exe")
            ElseIf (e.Result.Confidence > 0.9 And e.Result.Text = "open paint") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(1))
                    System.Diagnostics.Process.Start("mspaint")

                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try
            ElseIf (e.Result.Confidence > 0.8 And e.Result.Text = "close paint") Then
                Try

                    RichTextBox1.Text = ("Closing " & dyarr(1))
                    Shell("taskkill /im mspaint.exe")
                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try
            ElseIf (e.Result.Confidence > 0.1 And e.Result.Text = "goto google") Then
                RichTextBox1.Text = ("Opening " & dyarr(1))

                Process.Start("http://google.co.in")
            ElseIf (e.Result.Confidence > 0.2 And e.Result.Text = "goto facebook") Then
                RichTextBox1.Text = ("Opening " & dyarr(1))
                Shell("explorer " & "http://facebook.com")

            ElseIf (e.Result.Confidence > 0.7 And e.Result.Text = "goto yahoo") Then
                RichTextBox1.Text = ("Opening " & dyarr(1))
                Shell("explorer " & "http://yahoo.com")





            ElseIf (e.Result.Confidence > 0.8 And e.Result.Text = "goto apple") Then
                RichTextBox1.Text = ("Opening " & dyarr(1))

                Process.Start("http://apple.com")

            End If

        ElseIf (i = 1) Then
            If (e.Result.Confidence > 0.8 And e.Result.Text = "MyComputer") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(0))
                    Process.Start("explorer.exe", "/e,/select,c:")

                Catch ex As Exception

                End Try
            ElseIf (e.Result.Confidence > 0.1 And e.Result.Text = "Calculator") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(0))
                    Shell("calc")
                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try

            ElseIf (e.Result.Confidence > 0.1 And e.Result.Text = "C-Drive") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(0))

                    Process.Start("c:\")
                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try


            ElseIf (e.Result.Confidence > 0.7 And e.Result.Text = "D-Drive") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(0))
                    Process.Start("d:\")

                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try

            ElseIf (e.Result.Confidence > 0.5 And e.Result.Text = "C-Panel") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(0))
                    System.Diagnostics.Process.Start("control.exe")
                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try

            ElseIf (e.Result.Confidence > 0.01 And e.Result.Text = "TaskManager") Then 'spelled TuskManager
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(0))
                    System.Diagnostics.Process.Start("taskmgr")

                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try
            ElseIf (e.Result.Confidence > 0.2 And e.Result.Text = "CommandPrompt") Then
                Try
                    RichTextBox1.Text = ("Opening " & dyarr(0))
                    Process.Start("C:\Windows\system32\cmd.exe")


                Catch ex As Exception
                    RichTextBox1.Text = (ex.Message)
                End Try
            End If
        End If



    End Sub
    ' recognition failed event
    Public Sub recfailevent(ByVal sender As System.Object, ByVal e As RecognitionEventArgs)
        RichTextBox1.Text = "What was that?"

    End Sub
    Public Sub auevent(ByVal sender As System.Object, ByVal e As AudioLevelUpdatedEventArgs)

        ProgressBar1.Value = e.AudioLevel
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        '   recog = New SpeechRecognitionEngine(New System.Globalization.CultureInfo("en-US"))
        recog = New SpeechRecognitionEngine()
        recog.SetInputToDefaultAudioDevice()


        Dim commandChoices As New Choices("wordpad", "notepad", "paint")
        Dim grammarBuilder As New GrammarBuilder(New Choices("open", "close"))

        grammarBuilder.Append(commandChoices)


        Dim commandChoices1 As New Choices("google", "facebook", "yahoo", "apple")
        Dim grammarBuilder1 As New GrammarBuilder(New Choices("goto"))
        grammarBuilder1.Append(commandChoices1)
        Dim grammarbuilder2 As New GrammarBuilder(New Choices("Calculator", "TaskManager", "MyComputer", "CommandPrompt", "C-Panel", "D-Drive", "C-Drive"))

        Dim g As New Grammar(New Choices(grammarBuilder, grammarBuilder1, grammarbuilder2))
        g.Name = "Available programs"
        recog.LoadGrammarAsync(g)

        recog.RecognizeAsync(RecognizeMode.Multiple)



        ' add handlers for the recognition events
        AddHandler recog.AudioLevelUpdated, AddressOf Me.auevent
        AddHandler recog.SpeechRecognized, AddressOf Me.recevent
        AddHandler recog.SpeechRecognitionRejected, AddressOf Me.recfailevent
        ' enable the recogniser
        RichTextBox1.Text = ("Started")
    End Sub
    ''stop button
    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
        Try
            recog.RecognizeAsyncStop()
            recog.UnloadAllGrammars()
            ProgressBar1.Value = 0
            RichTextBox1.Text = ("recognition stopped")
        Catch ex As Exception
            RichTextBox1.Text = ("recognition not started")
        End Try


    End Sub












    ''minimize button
    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click
        Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
    End Sub
    ''close button
    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
        Me.Close()

    End Sub



















    ''mouse drag






    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub




    'Dim objicon As New Icon("C:\Users\sud\Documents\Visual Studio 2010\Projects\WindowsApplication4\WindowsApplication4\ico12.ico")

    Private Sub SpeechRecognition_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = New Icon("C:\Users\sud\Documents\Visual Studio 2010\Projects\WindowsApplication4\WindowsApplication4\icon13.ico")
        'Me.Icon = New Icon("C:\Program Files (x86)\Speak\SpeechRecognition")


        'Application.Run()

    End Sub
End Class